<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/11/20
 * Time: 09:47
 */

namespace Admin\Controller;


use Admin\Api\DatatreeApi;
use Common\Api\BoyeServiceApi;
use Library\Model\ZjportUploadHisModel;
use Shop\Api\OrdersApi;
use Shop\Api\OrdersItemApi;
use Library\Api\ZjportUploadHisApi;

class ZjportController  extends AdminController
{



    /**
     *
     * 支付单上报
     * @author hebidu <hebiduhebi@126.com>
     * @date  15/11/23 11:08
     * @copyright by itboye.com  
     */ 
    public function uploadPay()
    {
        if (IS_POST) {

            $order_code = $this->_post("order_code","","缺失订单编号!");
            $haiguan = $this->_post("haiguan","","上报海关必须");
            $data = array(
                'order_code'=>$order_code,
                'haiguan'=>$haiguan
            );

            $remoteSrv = new BoyeServiceApi();
            $result = $remoteSrv->callRemote("ZjportPay/upload",$data);
            if($result['status']){
                $info = $result['info'];
                if($info['code'] == 0){
                    $this->success($info['data']);
                }else{
                    $this->error($info['data']);
                }
            }else{
                $this->error($result['info']);
            }

        } else {

            $order_code = $this->_get("order_code","","缺失订单编号!");
            $map = array(
                'order_code'=>$order_code,
            );
            $result = apiCall(OrdersApi::GET_INFO,array($map));

            $this->checkApiCallResult($result);
            $this->assign("order",$result['info']);


            $map['type'] = ZjportUploadHisModel::UPLOAD_PAY_TYPE;

            $result = apiCall(ZjportUploadHisApi::QUERY,array($map,array('curpage'=>I('get.p',1),'size'=>10),"upload_time desc"));


            $this->assign("list",$result['info']['list']);
            $this->assign("show",$result['info']['show']);

            $this->display();
        }
    }

    /**
     *
     * 上报个人物品单
     * @author hebidu <hebiduhebi@126.com>
     * @date  15/11/20 09:48
     * @copyright by itboye.com
     */
    public function uploadPersonal()
    {
        if (IS_POST) {
            $businessNo = $this->_post("businessNo","");
            if(empty($businessNo)){
                $businessNo = $this->getBusinessNo();
            }
            $order_code = $this->_post("order_code","","缺失订单编号!");
            $destinationPort = $this->_post("destination_port","","抵运港必须!");
            $declPort = $this->_post("decl_port",0,"申报口岸必须");
            $packNo = $this->_post("pack_no",0,"表体独立包装的数目");
            $trafMode = $this->_post("traf_mode",'',"运输方式必须");
            $iePort = $this->_post("ie_port",'',"进出口岸必须");
            $tradeCountry = $this->_post("trade_country",'',"贸易国别(起运地)必须");
            $customsField = $this->_post("customs_field",'',"码头/货场必须");
            $applicationFormNo = $this->_post("application_form_no",'',"申请单编号");
            $declareCompanyType = $this->_post("declareCompanyType",'');
            $importType = $this->_post("importType",'');


            $data = array(
                'importType'=>$importType,
                'declareCompanyType'=>$declareCompanyType,
                'businessNo'=>$businessNo,
                'order_code'=>$order_code,
                'destination_port'=>$destinationPort,
                'decl_port'=>$declPort,
                'pack_no'=>$packNo,
                'traf_mode'=>$trafMode,
                'ie_port'=>$iePort,
                'trade_country'=>$tradeCountry,
                'customs_field'=>$customsField,
                'application_form_no'=>$applicationFormNo,
            );

            $remoteSrv = new BoyeServiceApi();
            $result = $remoteSrv->callRemote("Zjport/personal",$data);
            if($result['status']){
                $info = $result['info'];
                if(isset($info['code']) && $info['code'] == 0){
                    $this->success($info['data']['stringOfResultDetail']);
                }else{
                    if(isset($info['msg'])){
                        $this->error($info['msg']);
                    }else{
                        $this->error($info['data']);
                    }
                }
            }else{
                $this->error($result['info']);
            }

        } else {
            $order_code = $this->_get("order_code","","缺失订单编号!");
            $businessNo = $this->_get("businessNo","");
            $this->assign("businessNo",$businessNo);
            $map = array(
                'order_code'=>$order_code,
            );

            $result = apiCall(OrdersApi::GET_INFO,array($map));
            $this->checkApiCallResult($result);
            $this->assign("order",$result['info']);

            $map['type'] = ZjportUploadHisModel::UPLOAD_GOODSD_INFO;

            $result = apiCall(ZjportUploadHisApi::QUERY,array($map,array('curpage'=>I('get.p',1),'size'=>10),"upload_time desc"));


            $this->assign("list",$result['info']['list']);
            $this->assign("show",$result['info']['show']);

            $map['type'] = ZjportUploadHisModel::UPLOAD_CALLBACK_RESULT;

            $result = apiCall(ZjportUploadHisApi::QUERY,array($map,array('curpage'=>I('get.p',1),'size'=>10),"upload_time desc"));


            $this->assign("callback_list",$result['info']['list']);
            $this->assign("callback_show",$result['info']['show']);
            //1. 抵运港
            $map = array(
                'parentid'=>getDatatree("PORT_OF_SHIPMENT")
            );
            $result = apiCall(DatatreeApi::QUERY_NO_PAGING,array($map));
            $this->assign("port_of_shipment",$result['info']);

            //2. 申报口岸
            $map = array(
                'parentid'=>getDatatree("DECL_POST")
            );
            $result = apiCall(DatatreeApi::QUERY_NO_PAGING,array($map));

            $this->assign("decl_port_list",$result['info']);

            //3. 数目
            $result = apiCall(OrdersItemApi::SUM,array(array('order_code'=>$order_code),"count"));

            $this->assign("pack_no",$result['info']);

            //4. 运输方式
            $map = array(
                'parentid'=>getDatatree("TRAF_MODE")
            );
            $result = apiCall(DatatreeApi::QUERY_NO_PAGING,array($map));

            $this->assign("traf_mode_list",$result['info']);

            //5. 起运地国别代码
            $map = array(
                'parentid'=>getDatatree("COUNTRY")
            );
            $result = apiCall(DatatreeApi::QUERY_NO_PAGING,array($map));

            $this->assign("country_list",$result['info']);

            //6. 码头/货场代码
            $map = array(
                'parentid'=>getDatatree("CUSTOMS_FIELD")
            );
            $result = apiCall(DatatreeApi::QUERY_NO_PAGING,array($map));

            $this->assign("customs_field_list",$result['info']);

            $this->display();
        }
    }


    private function getBusinessNo(){

        $no = "5940".date("YmdHis",time());

        return $no;
    }

    /**
     *
     * 上报订单
     * @author hebidu <hebiduhebi@126.com>
     * @date  15/11/20 09:48
     * @copyright by itboye.com  
     */ 
    public function uploadOrder(){
        if(IS_POST){
            $order_code = $this->_post("order_code","","缺失订单编号!");
            $item_count = $this->_post("item_count",0,"商品信息缺失!");

            if(empty($item_count)){
                $result = apiCall(OrdersItemApi::SUM,array(array('order_code'=>$order_code),"count"));
                $item_count = $result['info'];
            }

            $data = array(
                'order_code'=>$order_code,
                'item_count'=>$item_count,
            );
            $remoteSrv = new BoyeServiceApi();
            $result = $remoteSrv->callRemote("Zjport/order",$data);
            if($result['status']){
                $info = $result['info'];
                if(isset($info['code']) && $info['code'] == 0){
                    $this->success($info);
                }else{
                    if(isset($info['msg'])){
                        $this->error($info['msg']);
                    }else{
                        $this->error($info['data']);
                    }
                }
            }else{
                $this->error($result['info']);
            }

        }else{
            $order_code = $this->_get('order_code','','缺失订单编号!');
            $map = array(
                'order_code'=>$order_code,
            );
            $result = apiCall(OrdersApi::GET_INFO,array($map));
            $this->checkApiCallResult($result);
            $this->assign("order",$result['info']);

            $result = apiCall(OrdersItemApi::SUM,array(array('order_code'=>$order_code),"count"));

            $this->assign("item_count",$result['info']);



            $map['type'] = ZjportUploadHisModel::UPLOAD_ORDER_INFO;
            $result = apiCall(ZjportUploadHisApi::QUERY,array($map,array('curpage'=>I('get.p',1),'size'=>10),"upload_time desc"));


            $this->assign("list",$result['info']['list']);
            $this->assign("show",$result['info']['show']);
            $this->display();
        }
    }

}